<html>
    <head>
        <script src="https://cdn.jsdelivr.net/npm/vue@2.6.12"></script>
    </head>
    <body>
        <div id="app">
           <h1>当前时间:{{date|formateTime}}</h1>
        </div>
        <script>
            //格式化后的时间格式：年-月-日 时：分：秒
            var parseDate=function(datetime){
                return datetime<10?'0'+datetime:datetime;
            }
           var app= new Vue({
                el:'#app',
                data:{
                    date:new Date()
                },
                filters:{
                        formateTime:function(val){
                            var date=new Date(val);
                            var year=date.getFullYear();
                            var month=parseDate(date.getMonth()+1);
                            var day=parseDate(date.getDate());
                            var hours=parseDate(date.getHours());
                            var min=parseDate(date.getMinutes());
                            var sec=parseDate(date.getSeconds());
                            return year+'-'+month+'-'+day+' '+hours+':'+min+":"+sec;
                        }
                },
                computed:function(){
                    var that=this;//作用域一致
                    this.timer=setInterval(function(){
                        that.date=new Date();
                    },
                    1000
                    );
                },
                beforeDestroy:function(){
                    if(this.timer){
                        clearInterval(this.timer);
                    }
                }
            })
        </script>
    </body>
</html>